/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.AreaBean;
import com.minierp.bean.AreaComparator;
import com.minierp.dao.AreaDao;
import com.minierp.dao.IdGenerator;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.Area;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class AreaFormCtr extends SimpleFormController {
	private AreaDao areaDao;
	private IdGenerator idGenerator;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String id = request.getParameter("id");
		if (id == null) {
			id = "";
		}

		refData.put("id", id);
		refData.put("areaList", this.areaDao.getList(true));
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		AreaBean bean = new AreaBean();
		Area form;
		if (id != null && !id.equals("")) {
			form = (Area) this.areaDao.get(Area.class, Long.parseLong(id));
			bean.setArea(form);
			bean.setOldSequCode(form.getSequCode());
		} else {
			form = new Area();
			String parentId = request.getParameter("parent_id");
			String sequCode = "";
			if (parentId != null && !parentId.equals("")
					&& !parentId.equals("0")) {
				Area list2 = (Area) this.areaDao.get(Area.class,
						Long.parseLong(parentId));
				List list1 = this.areaDao.getList(Long.parseLong(parentId),
						true);
				if (list2 != null) {
					form.setParentId(list2.getId());
					sequCode = list2.getSequCode() + "." + (list1.size() + 1);
				}
			} else {
				List list = this.areaDao.getList(0L, true);
				sequCode = String.valueOf(list.size() + 1);
			}

			form.setSequCode(sequCode);
			bean.setArea(form);
		}

		return bean;
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		String id = request.getParameter("id");
		AreaBean bean = (AreaBean) command;
		Area obj = bean.getArea();
		String alertMsg = "";
		String oldParentIdStr = "";
		if (obj.getParentIdStr() != null && !obj.getParentIdStr().equals("")) {
			oldParentIdStr = obj.getParentIdStr();
		}

		String parentIdStr = "";
		if (obj.getParentId() != 0L) {
			Area mv = (Area) this.areaDao.get(Area.class, obj.getParentId());
			if (mv != null) {
				if (mv.getParentIdStr() != null
						&& !mv.getParentIdStr().equals("")) {
					parentIdStr = mv.getParentIdStr() + mv.getId() + ";";
				} else {
					parentIdStr = ";" + mv.getId() + ";";
				}
			}
		}

		obj.setParentIdStr(parentIdStr);
		if (id != null && !id.equals("")) {
			this.areaDao.update(obj);
			List arg16 = this.areaDao.getAllChildren(obj.getId());
			Object[] continueFlag = arg16.toArray();
			if (bean.getOldSequCode() == null
					|| !bean.getOldSequCode().equals(obj.getSequCode())) {
				Arrays.sort(continueFlag, new AreaComparator());
				this.setChildSequCode(continueFlag, obj);
			}

			Area child;
			if (!oldParentIdStr.equals(parentIdStr)) {
				String i = "";
				if (oldParentIdStr.equals("")) {
					i = ";" + obj.getId() + ";";
				} else {
					i = oldParentIdStr + obj.getId() + ";";
				}

				String newStr = "";
				if (parentIdStr.equals("")) {
					newStr = ";" + obj.getId() + ";";
				} else {
					newStr = parentIdStr + obj.getId() + ";";
				}

				for (int i1 = 0; i1 < continueFlag.length; ++i1) {
					child = (Area) continueFlag[i1];
					child.setParentIdStr(child.getParentIdStr().replaceAll(i,
							newStr));
				}
			}

			for (int arg19 = 0; arg19 < continueFlag.length; ++arg19) {
				child = (Area) continueFlag[arg19];
				this.areaDao.update(child);
			}

			alertMsg = "修改成功！";
		} else {
			obj.setId(this.idGenerator.getId(Area.class.getName()));
			this.areaDao.save(obj);
			alertMsg = "增加成功！";
		}

		String arg18 = request.getParameter("continue_flag");
		ModelAndView arg17;
		if (arg18 != null && arg18.equals("1")) {
			arg17 = new ModelAndView("redirect:/base/AreaNew.ihtm?parent_id="
					+ obj.getParentId());
		} else {
			arg17 = new ModelAndView(this.getSuccessView());
			arg17.addObject("returnValue", String.valueOf(obj.getId()));
		}

		EncCommonHelper.setAlertMessage(request, alertMsg);
		return arg17;
	}

	private void setChildSequCode(Object[] objs, Area parent) {
		int index = 0;

		for (int i = 0; i < objs.length; ++i) {
			Area child = (Area) objs[i];
			if (child.getParentId() == parent.getId()
					&& child.getEnabled().equals("Y")) {
				++index;
				child.setSequCode(parent.getSequCode() + "." + index);
				this.setChildSequCode(objs, child);
			}
		}

	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public AreaDao getAreaDao() {
		return this.areaDao;
	}

	public void setAreaDao(AreaDao areaDao) {
		this.areaDao = areaDao;
	}
}